cmake_minimum_required(VERSION 2.8)

enable_language (Fortran)

project (farrow Fortran)

# make sure that the default is a RELEASE
if (NOT CMAKE_BUILD_TYPE)
  set (CMAKE_BUILD_TYPE RELEASE CACHE STRING
      "Choose the type of build, options are: None Debug Release."
      FORCE)
endif (NOT CMAKE_BUILD_TYPE)

# default installation
get_filename_component (default_prefix ".." ABSOLUTE)
set (CMAKE_INSTALL_PREFIX ${default_prefix} CACHE STRING
      "Choose the installation directory; by default it installs in the Farrow directory."
      FORCE)

# FFLAGS depend on the compiler
get_filename_component (Fortran_COMPILER_NAME ${CMAKE_Fortran_COMPILER} NAME)

if (Fortran_COMPILER_NAME STREQUAL "gfortran")
  # gfortran
  set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3")
  set (CMAKE_Fortran_FLAGS_DEBUG   "-fno-f2c -O0 -g")
elseif (Fortran_COMPILER_NAME STREQUAL "ifort")
  # ifort (untested)
  set (CMAKE_Fortran_FLAGS_RELEASE "-f77rtl -O3")
  set (CMAKE_Fortran_FLAGS_DEBUG   "-f77rtl -O0 -g")
elseif (Fortran_COMPILER_NAME STREQUAL "g77")
  # g77
  set (CMAKE_Fortran_FLAGS_RELEASE "-funroll-all-loops -fno-f2c -O3 -m32")
  set (CMAKE_Fortran_FLAGS_DEBUG   "-fno-f2c -O0 -g -m32")
else (Fortran_COMPILER_NAME STREQUAL "gfortran")
  message ("CMAKE_Fortran_COMPILER full path: " ${CMAKE_Fortran_COMPILER})
  message ("Fortran compiler: " ${Fortran_COMPILER_NAME})
  message ("No optimized Fortran compiler flags are known, we just try -O2...")
  set (CMAKE_Fortran_FLAGS_RELEASE "-O2")
  set (CMAKE_Fortran_FLAGS_DEBUG   "-O0 -g")
endif (Fortran_COMPILER_NAME STREQUAL "gfortran")
set(CMAKE_BUILD_TYPE debug)
set(CMAKE_Fortran_FLAGS_DEBUG "-O0 -fdefault-real-8 -ffixed-line-length-128")

#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${farrow_SOURCE_DIR}/cmake/Modules/")

set(pfu_dir "/opt/pFUnit")
set(pfu_lib "/opt/pFUnit/lib")
set(pfu_inc "/opt/pFUnit/mod")
#find_package(pFUnit REQUIRED)

set(INCS ${INCS} ${pfu_inc})
set(LIBS ${LIBS} ${pfu_lib})

include_directories(${INCS})
link_directories(${LIBS})

set(vtk_obj LIB_VTK_IO.f90)

add_library(vtk ${vtk_obj})

add_subdirectory("Mesh")

set(glob_src ${glob_src}
             domain.f90
             global.f90
             io.f90
             memory.f90
             point.f90
             triangle.f90
             utils_misc.f90
             variable.f90
)

set(glob_main main.f90)

set(test_src test_main.f90
             test_base.f90
             test_point.f90
             test_triangle.f90
             test_mesh.f90)

add_library(libfarrow STATIC ${glob_src} ${vtk_obj})
set_target_properties(libfarrow PROPERTIES OUTPUT_NAME farrow LINKER_LANGUAGE Fortran)

add_executable(farrow ${glob_main})
set_target_properties(farrow PROPERTIES LINKER_LANGUAGE Fortran)
target_link_libraries(farrow libfarrow vtk)

add_executable(test ${test_src})
set_target_properties(test PROPERTIES LINKER_LANGUAGE Fortran)
target_link_libraries(test libfarrow vtk pfunit)

